(function($) {
$.fn.ad = function(options){
    var settings = {
		adURL : '/ad/getAdList.asp',
		adList : null,
        code: ''
    };
    var opts = $.extend({},settings, options);
	return this.each(function(){
		$this = $(this);
		var o = $this.metadata ? $.extend(opts, $this.metadata()) : opts;
		getAdList($this,o);
	});
};

function getAdList($this,o){
	$.ajax({
		url:o.adURL+"?code="+o.code,
		dataType:"json", 
		success: function(json){
			o.adList=json;
			addAdList($this,o);
		},  
		error: function(){return null;}  
	});
}
function getRandomNum(min,max){
    var range = max - min + 1;
    var rand = Math.random();
    return(Math.floor((rand * range)) + min);
}

function addAdList($this,o){
	var el = o.adList;
	var html =[];
	var onmouse = '';
	$.each(el,function(i){
		var num = getRandomNum(0,el.length - 1);
		tmp = el[i];
		el[i] = el[num];
		el[num] = tmp
	});

	$.each(el,function(i){
		html.push('<div class=\"adlayer\"><ul><li><a href="'+el[i].direct+'" target="_blank"><img src="'+el[i].pic+'" border="0" /><span>');
		html.push('<dl class=\"adname\"><strong>'+el[i].name+'</strong></dl><dl class=\"adnote\">'+el[i].note+'</dl><dl class=\"adtel\">'+el[i].tel+'</dl>');
		html.push('</span></a></li></ul></div>');
	});
	$this.append(html.join(""));
	//addAdHoverEvent($this);
}

function addAdHoverEvent(){
	$(".adlayer").hover(
		function () {
			$(this).children().eq(0).css({opacity:0,display:'none'});
			$(this).children().eq(1).css({opacity:1,display:'block'});
		},
		function () {
			$(this).children().eq(1).css({opacity:0,display:'none'});
			$(this).children().eq(0).css({opacity:1,display:'block'});
		}
	);
}

})(jQuery);